package com.studentexam.student.service.impl;
import com.studentexam.student.dao.AnswerMapper;
import com.studentexam.student.dao.ExamMapper;
import com.studentexam.student.dao.ExamQuestionMapper;
import com.studentexam.student.model.Exam;
import com.studentexam.student.service.ExamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ExamServiceImpl implements ExamService {


    @Autowired
    private ExamMapper examMapper;
    @Autowired
    private AnswerMapper answerMapper;
    @Autowired
    private ExamQuestionMapper examQuestionMapper;


    @Override
    public int addExam(Exam exam) {
        int returnInt=examMapper.insertExam(exam);

        return returnInt;
    }

    @Override
    public int updateExam(Exam exam) {
        int isUpdate=0;
        try{
            isUpdate=examMapper.updateExam(exam);
        }catch (Exception e){
            isUpdate=-1;
            System.out.println("更新试题信息时执行sql报错原因为:"+e.getMessage());
        }
        return isUpdate;
    }

    @Override
    public int delExam(int examid) {
        int returnDel=0;
        try {
            if (answerMapper.getExamCount(examid)< 1&examQuestionMapper.getExamCount(examid)<1) {
                returnDel = examMapper.delExam(examid);
            }
        }catch (Exception e){
            returnDel=-1;
            System.out.println("删除失败原因为："+e.getMessage());
        }
        return returnDel;
    }

    @Override
    public List<Exam> getByExamid(int examid) {
        List<Exam> examList=examMapper.getByExamid(examid);
        return examList;
    }

    @Override
    public List<Exam> getAllExam(Exam exam){
        List<Exam> examList = examMapper.getAllExam(exam);
        return examList;
    }
}
